function cor(proj_meta,cur_site)

bas_results=[];
pb_results=[];
fb_results=[];
ffm_results=[];
lfm_results=[];
pbffm_results=[];
pblfm_results=[];
ids=14:26;
ids(ids==20)=[];
ids2=34:46;
ids2(ids2==40)=[];
for tnd=1:length(proj_meta(cur_site).ExpGroup)
    cnt2=1;
    for gnd=1:proj_meta(cur_site).nbr_piezo_layers
        nbr_frames=proj_meta(cur_site).raw_data(gnd,tnd).nbr_frames;
        order=[];
        if length(nbr_frames)>5
            for xnd=1:length(nbr_frames)
                if nbr_frames(xnd)==5000
                    if xnd==length(nbr_frames)
                        order(xnd)=3;
                    else
                        order(xnd)=1;
                    end
                else
                    order(xnd)=2;
                end
            end
            ord_ind=1:length(order);
            order(max(ord_ind(order==2))+1:end)=3;
        else
            order=[1 2 1 2 3];
        end
        exp_inds=[];
        exp_inds(1,1)=1;
        exp_inds(1,2)=nbr_frames(1);
        for fnd=2:length(order)
            exp_inds(fnd,1)=exp_inds(fnd-1,2)+1;
            exp_inds(fnd,2)=sum(nbr_frames(1:fnd));
        end
        stim_id=proj_meta(cur_site).raw_data(gnd,tnd).stim_id;
        runM=proj_meta(cur_site).raw_data(gnd,tnd).velM_ind;
        runP=proj_meta(cur_site).raw_data(gnd,tnd).velP_ind;
        vis_fl=[];
        shad=[];
        cnt=1;
        for bnd=1:length(order)
            if order(bnd)==1
                tmp_runM=runM(exp_inds(bnd,1):exp_inds(bnd,2));
                tmp_stim_id=stim_id(exp_inds(bnd,1):exp_inds(bnd,2));
                for fnd=1:length(tmp_runM)
                    if tmp_runM(fnd)==0
                        shad(cnt)=0;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    elseif tmp_runM(fnd)==1 & logical(tmp_stim_id(fnd))==0 %feedback
                        shad(cnt)=1;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    elseif tmp_runM(fnd)==1 & logical(tmp_stim_id(fnd))==1 %feedback mismatch
                        if tmp_stim_id(fnd)==7
                            shad(cnt)=3;
                            vis_fl(cnt)=0;
                            cnt=cnt+1;
                        else
                            shad(cnt)=20+tmp_stim_id(fnd);
                            vis_fl(cnt)=1;
                            cnt=cnt+1;
                        end
                    end
                end
            elseif order(bnd)==2
                tmp_runM=runM(exp_inds(bnd,1):exp_inds(bnd,2));
                tmp_stim_id=stim_id(exp_inds(bnd,1):exp_inds(bnd,2));
                tmp_runP=runP(exp_inds(bnd,1):exp_inds(bnd,2));
                for fnd=1:length(tmp_runM)
                    if logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0 | logical(tmp_stim_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0
                        shad(cnt)=0;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    elseif logical(tmp_stim_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                        if tmp_stim_id(fnd)==7
                            shad(cnt)=5;
                            vis_fl(cnt)=0;
                            cnt=cnt+1;
                        else
                            shad(cnt)=40+tmp_stim_id(fnd);
                            vis_fl(cnt)=1;
                            cnt=cnt+1;
                        end
                    elseif logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                        shad(cnt)=2;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    elseif logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                        shad(cnt)=1;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    elseif logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0 | logical(tmp_stim_id(fnd))==1 && tmp_runM(fnd)==1 & tmp_runP(fnd)==0
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    elseif logical(tmp_stim_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                        if tmp_stim_id(fnd)==7
                            shad(cnt)=3;
                            vis_fl(cnt)=0;
                            cnt=cnt+1;
                        else
                            shad(cnt)=20+tmp_stim_id(fnd);
                            vis_fl(cnt)=1;
                            cnt=cnt+1;
                        end
                    end
                end
            end
        end
%         vis_fl(end+1:end+(length(runP)-length(vis_fl)))=0;
%         shad(end+1:end+(length(runP)-length(shad)))=0;
        %         cur_run=proj_meta(cur_site).raw_data(gnd,tnd).velM_ind;
        for fnd=1:size(proj_meta(cur_site).raw_data(gnd,tnd).smoothed_ROIs,2)
            cur_act=proj_meta(cur_site).raw_data(gnd,tnd).smoothed_ROIs(fnd).activity/median(proj_meta(cur_site).raw_data(gnd,tnd).smoothed_ROIs(fnd).activity);
            
%             pb_results(tnd,cnt2)=corr(cur_act,vis_fl');
            cur_act=cur_act(1:length(shad));
            bas_results(tnd,cnt2)=corr(cur_act,(shad==0)');
            fb_results(tnd,cnt2)=corr(cur_act,(shad==1)');
            pb_results(tnd,cnt2)=corr(cur_act,(shad==2)');
            ffm_results(tnd,cnt2)=corr(cur_act,(shad==3)');
            for ynd=1:12
                lfm_results(ynd,cnt2,tnd)=corr(cur_act,(shad==ids(ynd))');
                pblfm_results(ynd,cnt2,tnd)=corr(cur_act,(shad==ids2(ynd))');
            end
            pbffm_results(tnd,cnt2)=corr(cur_act,(shad==5)');
            cnt2=cnt2+1;
        end
    end
end

% run_results=[];
% for tnd=1:length(proj_meta(cur_site).ExpGroup)
%     cnt2=1;
%     for gnd=1:proj_meta(cur_site).nbr_piezo_layers
%         cur_run=proj_meta(cur_site).raw_data(gnd,tnd).velM_ind;
%         for fnd=1:size(proj_meta(cur_site).raw_data(gnd,tnd).smoothed_ROIs,2)
%             cur_act=proj_meta(cur_site).raw_data(gnd,tnd).smoothed_ROIs(fnd).activity/median(proj_meta(cur_site).raw_data(gnd,tnd).smoothed_ROIs(fnd).activity);
%             run_results(tnd,cnt2)=(corr(cur_act,cur_run'))^2;
%             cnt2=cnt2+1;
%         end
%     end
% end
% 
% final_res=[];
% for tnd=1:length(proj_meta(cur_site).ExpGroup)
%     for wnd=1:cnt2-1
%         if pb_results(tnd,wnd)<0.081 & run_results(tnd,wnd)<0.081 & ffm_results(tnd,wnd)<0.081 & lfm_results(tnd,wnd)<0.081
%             final_res(tnd,wnd)=0;
%         else
%             if pb_results(tnd,wnd)==max([pb_results(tnd,wnd) run_results(tnd,wnd) ffm_results(tnd,wnd) lfm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=1;
%             elseif run_results(tnd,wnd)==max([pb_results(tnd,wnd) run_results(tnd,wnd) ffm_results(tnd,wnd) lfm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=2;
%             elseif ffm_results(tnd,wnd)==max([pb_results(tnd,wnd) run_results(tnd,wnd) ffm_results(tnd,wnd) lfm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=3;
%             elseif lfm_results(tnd,wnd)==max([pb_results(tnd,wnd) run_results(tnd,wnd) ffm_results(tnd,wnd) lfm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=4;
%             end
%         end
%     end
% end
% 
% final_res=[];
% for tnd=1:length(proj_meta(cur_site).ExpGroup)
%     for wnd=1:cnt2-1
%         if pb_results(tnd,wnd)<0.081 & fb_results(tnd,wnd)<0.081 & ffm_results(tnd,wnd)<0.081 & pbffm_results(tnd,wnd)<0.081
%             final_res(tnd,wnd)=0;
%         else
%             if pb_results(tnd,wnd)==max([pb_results(tnd,wnd) fb_results(tnd,wnd) ffm_results(tnd,wnd) pbffm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=2;
%             elseif fb_results(tnd,wnd)==max([pb_results(tnd,wnd) fb_results(tnd,wnd) ffm_results(tnd,wnd) pbffm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=1;
%             elseif ffm_results(tnd,wnd)==max([pb_results(tnd,wnd) fb_results(tnd,wnd) ffm_results(tnd,wnd) pbffm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=3;
%             elseif pbffm_results(tnd,wnd)==max([pb_results(tnd,wnd) fb_results(tnd,wnd) ffm_results(tnd,wnd) pbffm_results(tnd,wnd)])
%                 final_res(tnd,wnd)=4;
%             end
%         end
%     end
% end

conc_res=zeros(28,cnt2-1,length(proj_meta(cur_site).ExpGroup));
conc_res(1,:,:)=bas_results';
conc_res(2,:,:)=fb_results';
conc_res(3,:,:)=pb_results';
conc_res(4,:,:)=ffm_results';
conc_res(5,:,:)=pbffm_results';
conc_res(6:17,:,:)=lfm_results;
conc_res(18:29,:,:)=pblfm_results;

conc_res=conc_res.^2;

final_res=[];
for tnd=1:length(proj_meta(cur_site).ExpGroup)
    for wnd=1:cnt2-1
        cur_cell=conc_res(:,wnd,tnd);
        max_cor=max(cur_cell);
        if max_cor>0.081
            final_res(tnd,wnd)=find(cur_cell==max_cor);
        else
            final_res(tnd,wnd)=0;
        end
    end
end

count=[];
for tnd=1:length(proj_meta(cur_site).ExpGroup)
    for ynd=1:30
        count(tnd,ynd)=sum(final_res(tnd,:)==ynd-1);
    end
end

figure;
plot(count(:,3),'color',[0.5 0.5 0.5])
hold on
plot(count(:,4),'g')
plot(count(:,5),'color',[1 0.5 0])
